package com.zxw.user.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import com.zxw.common.core.base.constant.RedisConstants;
import com.zxw.common.core.base.constant.SysConstants;
import com.zxw.common.web.util.RedisCacheClient;
import com.zxw.user.service.EmailService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

import static com.zxw.common.core.base.constant.RedisConstants.CACHE_EMAIL_VERIFICATION_CODE_TTL;

/**
 * @author Xuewei.Zhang
 * @date 2022/12/2
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class EmailServiceImpl implements EmailService {


    private final StringRedisTemplate stringRedisTemplate;

    private final RedisCacheClient redisCacheClient;

    @Override
    public void sendVerificationCode(String email) {
        int code = RandomUtil.randomInt(1000, 9999);
//        stringRedisTemplate.opsForValue().set(RedisConstants.EMAIL_VERIFICATION + email, code + "", 5, TimeUnit.MINUTES);
        redisCacheClient.setBaseType(RedisConstants.EMAIL_VERIFICATION + email, code, CACHE_EMAIL_VERIFICATION_CODE_TTL, TimeUnit.MINUTES);
        log.info("准备发送");
        MailAccount mailAccount = SysConstants.SYS_EMAIL_MAP.get(SysConstants.EMAIL_126);
        log.info("开始发送");
        log.info(code + "");
        MailUtil.send(mailAccount, CollUtil.newArrayList(email), "验证码", code + "", false);
    }

    @Override
    public void send(String email, String content) {
        MailAccount mailAccount = SysConstants.SYS_EMAIL_MAP.get(SysConstants.EMAIL_126);
        MailUtil.send(mailAccount, CollUtil.newArrayList(email), "建议登陆后修改密码！重置后的密码：", content, false);
    }
}
